<?php
/**
 * Делает постинг сообщения в сообщество. Поидее из этого всего должна развиваться логика с правилами.
 * 
 * @author Alexander Sibiryakov
 */
class Treatment {
	public function __construct(Model_Item $mi, Model_AclAssistant $aa, $rfl, $rh) {
		$this->mi = $mi;
		$this->aa = $aa;
		$this->rfl = $rfl;
		$this->rh = $rh;
	}
	
	public function process($items, $feed_oid) {
		$comm_oids = $this->rfl->getCommOidsSubsrcibedToFeed($feed_oid);

		$feed = $this->mi->store->getObject($feed_oid);
		$theme = $this->mi->store->getObject($feed->data['parent_oid']);
		
		if (!$comm_oids) return;
		
		$ctx = new Object();
		$ctx->rh = $this->rh;
		$ctx->store = $this->mi->store;
		$this->rh->useClass("Filters_List");
    $filtersList = new Filters_List ( $ctx );
		$themesmodel = $this->mi->mm->getModel('Themes');
		$filters = $themesmodel->getFilters($theme->id);
		
		//print '<pre style="text-align:left;">'; print_r($filters);exit;
		
		$filtersObjects = array();
		if ( !empty($filters) ) {
			foreach ( $filters as $idx=>$val ) {
				$filtersObjects[$idx] = $filtersList->getFilterObject($filters[$idx]['filter']);
				$filtersObjects[$idx]->setData($filters[$idx]);
			}
		}
		
		//print '<pre style="text-align:left;">'; print_r($filtersObjects);exit;
    
    //error_reporting(E_ALL);
    //ini_set('display_errors', 1);
		
    $toSearch = array();
		foreach ($items as $rss_item) {
			foreach ($comm_oids as $coid) {
				$action = '';
				foreach ( $filtersObjects as $filter ) {
					$action = $filter->testItem( $rss_item );
					if ( !empty($action) ) {
						break;
					}
				}
				
				if ( !isset($_SERVER['DOCUMENT_ROOT']) ) {
					echo "rss_item {$action}<br />";
				}
				
				if ( 'kill' != $action ) {
					if ( 'moderated' == $action ) {
						$item = $this->mi->createItem("moderated_doc");
            
  					$item->data['beplaced_community_oid'] = $coid;
  					if ( $coid != $theme->data['parent_oid'] ) {
  						$item->data['beplaced_tdi_oid'] = $theme->data['parent_oid'];
  					}
					} else {
						$item = $this->mi->createItem("doc");
            
  					$item->data['community_oid'] = $coid;
  					if ( $coid != $theme->data['parent_oid'] ) {
  						$item->data['tdi_oid'] = $theme->data['parent_oid'];
  					}
            
            $toSearch[] = $item;
					}
          
					$item->data['title'] = html_entity_decode($rss_item->title);
					$item->data['body'] = $rss_item->description;
          
					$this->mi->upItem($item);
					$this->aa->createItem( $item->id, $coid );
				}
			}
		}
    
    if ( $this->mi->mm->rh->use_search && !empty($toSearch) ) {
      //error_reporting(E_ALL);
      //ini_set('display_errors', 1);
      include 'includes/workaholic.php';
      //$this->hashItem($toSearch);
      Workaholic::addTask(array($this, 'hashItem'), $toSearch);
    }
	}
  
  public function hashItem( $toSearch ) {
    foreach ( $toSearch as $item ) {
      $obj = $this->mi->store->getObject($item->id);
      
      $this->rh->useClass('Search_Types');
      $searchTypes = new Search_Types($this->rh, $this->mi->store);
      
      $parser = $searchTypes->getTypeParser($obj->type);
      if ( false === $parser ) {
        //print "Can`t find parser for '{$obj->type}' type.\n";
        //return;
        continue;
      }
      
      $parser->linkFromObject($obj);
      $maked = $parser->makeSearchData($obj->id);
      if ( !$maked ) {
        //print "Can`t index item with '{$obj->id}' oid.";
        //return;
        continue;
      }
    }
    
    //$parser->cleanWords();
  }
}

?>
